/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.userinfo.service.impl;

import cn.icanci.basic.commonutils.CommonUtils;
import cn.icanci.basic.exception.ApplicationException;
import cn.icanci.userinfo.entity.UserInfo;
import cn.icanci.userinfo.mapper.UserInfoMapper;
import cn.icanci.userinfo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

/**
 * @author icanci
 * @date Created in 2021/04/28 15:12
 * @version 1.0
 */
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 增加一个用户
     * @param userInfo 需要增加的用户对象
     * @return 返回影响的行数
     */
    @Override
    public int insertOneRow(UserInfo userInfo) {
        if (null == userInfo) {
            throw new ApplicationException(200001, "用户信息为null");
        }
        userInfo.setBlack(0);
        userInfo.setPassword(CommonUtils.MD5(userInfo.getPassword()));
        userInfo.setCreateTime(new Date());
        return userInfoMapper.insertOneRow(userInfo);
    }

    /**
     * 查询一个用户
     * @param userInfo 需要查询的用户对象
     * @return 返回查询的对象
     * @throws ApplicationException
     */
    @Override
    public UserInfo queryOneRow(UserInfo userInfo) throws ApplicationException {
        userInfo.setPassword(CommonUtils.MD5(userInfo.getPassword()));
        return userInfoMapper.queryOneRow(userInfo);
    }

    /**
     * 查询一个用户
     * @param userInfo 需要查询的用户对象
     * @return 返回查询的对象
     * @throws ApplicationException
     */
    @Override
    public UserInfo queryOneRowByPhone(UserInfo userInfo) throws ApplicationException {
        return userInfoMapper.queryOneRowByPhone(userInfo);
    }

    /**
     * 更新一个用户的信息
     * @param userInfo 用户信息
     * @return 返回影响的结果
     */
    @Override
    public int updateOneRow(UserInfo userInfo) {
        userInfo.setPassword(CommonUtils.MD5(userInfo.getPassword()));
        return userInfoMapper.updateOneRow(userInfo);
    }

    /**
     * 查询所有用户的信息
     * @return 返回影响的结果
     */
    @Override
    public List<UserInfo> queryAllUserInfo() {
        return userInfoMapper.queryAllUserInfo();
    }

    /**
     * 根据id查询用户的信息
     * @param id
     * @return
     */
    @Override
    public UserInfo queryOneUserInfo(Integer id) {
        return userInfoMapper.queryOneUserInfo(id);
    }
}
